<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="128px">
      <el-form-item label="所在供应商名称" prop="supplierid">
        <el-input
          v-model="queryParams.suppliername"
          placeholder="请输入所在供应商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆品牌" prop="brand">
        <el-input
          v-model="queryParams.brand"
          placeholder="请输入车辆品牌"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车辆类型" prop="model">
        <el-input
          v-model="queryParams.model"
          placeholder="请输入车辆类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车身颜色" prop="color">
        <el-input
          v-model="queryParams.color"
          placeholder="请输入车身颜色"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="进价" prop="primeprice">
        <el-input
          v-model="queryParams.primeprice"
          placeholder="请输入进价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['purchasevehicle:purchasevehicle:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['purchasevehicle:purchasevehicle:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['purchasevehicle:purchasevehicle:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['purchasevehicle:purchasevehicle:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="purchasevehicleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="车辆ID" align="center" prop="vehicleid" />-->

      <el-table-column type="expand" align="center">
        <template slot-scope="scope">
          <el-tag style="font-size: 13px;">
            生产日期：{{scope.row.manufacturedate}}
          </el-tag > ||

          <el-tag style="font-size: 13px;">
            发动机号：{{scope.row.enginenumber}}
          </el-tag > ||

          <el-tag style="font-size: 13px;">
            车架号：{{scope.row.vin}}
          </el-tag> ||

          <el-tag style="font-size: 13px;">
            创建时间：{{scope.row.createtime}}
          </el-tag> ||

          <el-tag style="font-size: 13px;">
            更新时间：{{scope.row.modifytime}}
          </el-tag> ||
          <br><br>
          <el-tag style="font-size: 13px;">
            核载人数：{{scope.row.seatingcapacity}}
          </el-tag> ||

          <el-tag style="font-size: 13px;">
            发动机类型：{{scope.row.enginetype}}
          </el-tag> ||

          <el-tag style="font-size: 13px;">
            排量：{{scope.row.enginedisplacement}}
          </el-tag> ||

          <el-tag style="font-size: 13px;">
            功率：{{scope.row.power}}
          </el-tag> ||

          <el-tag style="font-size: 13px;">
            车轴数量：{{scope.row.axlesnumber}}
          </el-tag> ||
          <br><br>
          <el-tag style="font-size: 13px;">
            车轴距离：{{scope.row.wheelbase}}
          </el-tag> ||

          <el-tag style="font-size: 13px;">
            前轮距：{{scope.row.fronttrack}}
          </el-tag> ||

          <el-tag style="font-size: 13px;">
            后轮距：{{scope.row.reartrack}}
          </el-tag> ||

          <el-tag style="font-size: 13px;">
            满载总质量：{{scope.row.grossvichcleweight}}
          </el-tag> ||

          <el-tag style="font-size: 13px;">
            汽车整备质量：{{scope.row.curbweight}}
          </el-tag>
        </template>
      </el-table-column>

      <el-table-column label="供应商名称" align="center" prop="suppliername" />
      <el-table-column label="车辆品牌" align="center" prop="brand" />
      <el-table-column label="车辆类型" align="center" prop="model" />
      <el-table-column label="车身颜色" align="center" prop="color" />
<!--      <el-table-column label="生产日期" align="center" prop="manufacturedate" />-->
<!--      <el-table-column label="发动机号" align="center" prop="enginenumber" />-->
<!--      <el-table-column label="车架号" align="center" prop="vin" />-->
      <el-table-column label="进价" align="center" prop="primeprice" />
<!--      <el-table-column label="售价" align="center" prop="price" />-->
<!--      <el-table-column label="创建时间" align="center" prop="createtime" />-->
<!--      <el-table-column label="更新时间" align="center" prop="modifytime" />-->
<!--      <el-table-column label="核载人数" align="center" prop="seatingcapacity" />-->
<!--      <el-table-column label="发动机类型" align="center" prop="enginetype" />-->
<!--      <el-table-column label="排量" align="center" prop="enginedisplacement" />-->
<!--      <el-table-column label="功率" align="center" prop="power" />-->
<!--      <el-table-column label="车轴数量" align="center" prop="axlesnumber" />-->
<!--      <el-table-column label="车轴距离" align="center" prop="wheelbase" />-->
<!--      <el-table-column label="前轮距" align="center" prop="fronttrack" />-->
<!--      <el-table-column label="后轮距" align="center" prop="reartrack" />-->
<!--      <el-table-column label="满载总质量" align="center" prop="grossvichcleweight" />-->
<!--      <el-table-column label="汽车整备质量" align="center" prop="curbweight" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="medium"
            type="text"
            icon="el-icon-shopping-cart-2"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['purchasevehicle:purchasevehicle:edit']"
          >生成订单</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['purchasevehicle:purchasevehicle:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改购买车辆对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="所属仓库" prop="warehouseid">
          <el-select v-model="form.warehouseid" placeholder="请输入所属仓库">
            <el-option
              v-for="item in warehouseList"
              :key="item.warehouseid"
              :label="item.warehousename"
              :value="item.warehouseid">
            </el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="所属仓库" prop="warehouseid">-->
<!--          <el-input v-model="form.warehouseid" placeholder="请输入所属仓库" />-->
<!--        </el-form-item>-->
        <el-form-item label="售价" prop="price">
          <el-input v-model="form.price" placeholder="请输入售价" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactnumber">
          <el-input v-model="form.contactnumber" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPurchasevehicle, getPurchasevehicle, delPurchasevehicle, addPurchasevehicle, updatePurchasevehicle } from "@/api/purchasevehicle/purchasevehicle";
import { listWarehouse } from "@/api/warehouse/warehouse";
import warehouse from "@/views/warehouse/warehouse/index.vue";

export default {
  name: "Purchasevehicle",
  data() {
    return {
      warehouseList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      warehouseid: {},
      // 购买车辆表格数据
      purchasevehicleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierid: null,
        brand: null,
        model: null,
        color: null,
        primeprice: null,
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        warehouseid: [
          { required: true, message: "仓库ID不能为空", trigger: "blur" }
        ],
        price: [
          { required: true, message: "售价不能为空", trigger: "blur" }
        ],
        contactnumber: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "地址不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询购买车辆列表 */
    getList() {
      this.loading = true;
      listPurchasevehicle(this.queryParams).then(response => {
        this.purchasevehicleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });

      listWarehouse().then(response => {
        this.warehouseList = response.rows;
      });

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        vehicleid: null,
        status: null,
        supplierid: null,
        warehouseid: null,
        customerid: null,
        salespersonid: null,
        brand: null,
        model: null,
        color: null,
        manufacturedate: null,
        enginenumber: null,
        vin: null,
        primeprice: null,
        price: null,
        createtime: null,
        modifytime: null,
        deleted: null,
        seatingcapacity: null,
        enginetype: null,
        enginedisplacement: null,
        power: null,
        axlesnumber: null,
        wheelbase: null,
        fronttrack: null,
        reartrack: null,
        grossvichcleweight: null,
        curbweight: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.vehicleid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加购买车辆";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const vehicleid = row.vehicleid;
      getPurchasevehicle(vehicleid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "生成车辆采购订单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.vehicleid != null) {
            console.log(this.form);
            updatePurchasevehicle(this.form).then(response => {
              this.$modal.msgSuccess("生成订单成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPurchasevehicle(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const vehicleids = row.vehicleid || this.ids;
      this.$modal.confirm('是否确认删除购买车辆编号为"' + vehicleids + '"的数据项？').then(function() {
        return delPurchasevehicle(vehicleids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('purchasevehicle/purchasevehicle/export', {
        ...this.queryParams
      }, `purchasevehicle_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
